#include "SoundWrap.h"


#if defined(TARGET_IPHONE_SIMULATOR) || defined(TARGET_OS_IPHONE)


#include "SoundEngine.h"
#include "Core/Commons.h"

#define MAX_FREQ					44100.0f


CSoundWrap::CSoundWrap()
{
	for (int i = 0; i < SOUND_NUM_FILES; i++)
		m_bLoaded[i] = false;
	m_bIsSoundAndMusicInitialized = false;

#ifdef USE_SKIP_PLAY_SOUNDS_AFTER_INTERRUPT	
	m_bSkipPlaySoundsAfterInterrupt = false;	
#endif	
}

CSoundWrap::~CSoundWrap()
{
	SoundEngine_Teardown();
}


//////////////////////////////////////////////////////////////////////////
//-----------------------------------------------------------------------------------------------------

// Setting sound volume
void CSoundWrap::SetMasterVolume( unsigned char vol )
{
    SoundEngine_SetMasterVolume(((float)(vol - MASTER_CONTROL_VOLUME_MIN)) / (MASTER_CONTROL_VOLUME_MAX - MASTER_CONTROL_VOLUME_MIN));
}

void CSoundWrap::SetSfxVolume( unsigned char vol )
{
#ifdef USE_SKIP_PLAY_SOUNDS_AFTER_INTERRUPT
	if( m_bSkipPlaySoundsAfterInterrupt )
		return;
#endif
	SoundEngine_SetEffectsVolume(((float)(vol - SOUND_CONTROL_VOLUME_MIN)) / (SOUND_CONTROL_VOLUME_MAX - SOUND_CONTROL_VOLUME_MIN));
}

void CSoundWrap::SetMusicVolume( unsigned char vol )
{
	SoundEngine_SetBackgroundMusicVolume(((float)(vol - MUSIC_CONTROL_VOLUME_MIN)) / (MUSIC_CONTROL_VOLUME_MAX - MUSIC_CONTROL_VOLUME_MIN));
}

// loading sounds
void CSoundWrap::SampleLoad( int idx, char* data, unsigned long length )
{
#ifdef USE_SKIP_PLAY_SOUNDS_AFTER_INTERRUPT
	if( m_bSkipPlaySoundsAfterInterrupt )
		return;
#endif
	unsigned long id;
	if (noErr != SoundEngine_LoadEffect(data, length, &id))
	{
		m_usSoundMapping[idx] = -1;
		m_bLoaded[idx] = false;
	}
	else
	{
		m_usSoundMapping[idx] = id;
		m_bLoaded[idx] = true;
	}
}

void CSoundWrap::SampleFree( int idx )
{
#ifdef USE_SKIP_PLAY_SOUNDS_AFTER_INTERRUPT
	if( m_bSkipPlaySoundsAfterInterrupt )
		return;
#endif
	if(m_bLoaded[idx])
		SoundEngine_UnloadEffect(m_usSoundMapping[idx]);
	m_bLoaded[idx] = false;
}

void CSoundWrap::SampleStart( int idx, bool loop, bool resetPitch )
{
	
#ifdef USE_SKIP_PLAY_SOUNDS_AFTER_INTERRUPT
	if( m_bSkipPlaySoundsAfterInterrupt )
		return;
#endif
	LOG("global", "sample Start %d ", idx);
	if(m_bLoaded[idx])
		SoundEngine_StartEffect(m_usSoundMapping[idx], loop);
}

void CSoundWrap::SampleStop( int idx )
{
#ifdef USE_SKIP_PLAY_SOUNDS_AFTER_INTERRUPT
	if( m_bSkipPlaySoundsAfterInterrupt )
		return;
#endif
	
	if(m_bLoaded[idx])
		SoundEngine_StopEffect (m_usSoundMapping[idx]);
}

bool CSoundWrap::SamplePlaying( int idx )
{
#ifdef USE_SKIP_PLAY_SOUNDS_AFTER_INTERRUPT
	if( m_bSkipPlaySoundsAfterInterrupt )
		return false;
#endif
	if(m_bLoaded[idx])
	    return SoundEngine_IsEffectPlaying (m_usSoundMapping[idx]);

	return false;
}

////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
/// Music functions 
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////

//
// ------------------------------------------------------------------------
// MusicLoad()
// Append new music to music list 
// For now if other music was loaded previously it has been stopped
// ------------------------------------------------------------------------
//
bool CSoundWrap::MusicLoad( /*const char* name*/ int name_idx ) 
{
	//if(!m_bIsSoundAndMusicInitialized) return false;

	//char filePath[1024];
	//strcpy(filePath, g_AppPath);
	//strcat(filePath, MUSIC_FILE[name_idx]);
	
	//SoundEngine_LoadBackgroundMusicTrack(filePath, false, false);	
	
	return true;		
}	

//
// ------------------------------------------------------------------------
// MusicStart()
// ------------------------------------------------------------------------
//
void CSoundWrap::MusicStart( bool loop ) 
{
	if(!m_bIsSoundAndMusicInitialized) return;
	SoundEngine_StartBackgroundMusic();
}

//
// ------------------------------------------------------------------------
// MusicStop()
// ------------------------------------------------------------------------
//
void CSoundWrap::MusicStop( ) 
{
	if(!m_bIsSoundAndMusicInitialized) return;
	SoundEngine_StopBackgroundMusic(false);
}		

//
// ------------------------------------------------------------------------
// MusicPause()
// ------------------------------------------------------------------------
//
void CSoundWrap::MusicPause( const bool pause ) 
{
}

//
// ------------------------------------------------------------------------
// MusicPlaying()
// Return true, if music was playing.
// ------------------------------------------------------------------------
//

bool CSoundWrap::MusicPlaying( )
{
#ifdef ENABLE_MUSIC
	if(!m_bIsSoundAndMusicInitialized) 
#endif//ENABLE_MUSIC_
		return false;

	return SoundEngine_IsMusicPlaying();
}

bool CSoundWrap::MusicPlaying( int id )
{
	return MusicPlaying();// && (id  == cGame::getGame()->m_currentMusicIndex);
}

//
// ------------------------------------------------------------------------
// MusicFree()
// ------------------------------------------------------------------------
//
void CSoundWrap::MusicFree() 
{
	if(!m_bIsSoundAndMusicInitialized) return;
	SoundEngine_UnloadBackgroundMusicTrack();
}	



#else /* ENABLE_SOUND */

CSoundWrap::CSoundWrap()
{
}

CSoundWrap::~CSoundWrap()
{
}


//////////////////////////////////////////////////////////////////////////
//-----------------------------------------------------------------------------------------------------

// Setting sound volume
void CSoundWrap::SetMasterVolume( unsigned char vol )
{
}

void CSoundWrap::SetSfxVolume( unsigned char vol )
{
}

void CSoundWrap::SetMusicVolume( unsigned char vol )
{
}

// loading sounds
void CSoundWrap::SampleLoad( int idx, char* data, unsigned long length )
{
}

void CSoundWrap::SampleFree( int idx )
{
 
}

void CSoundWrap::SampleStart( int idx, bool loop, bool resetPitch )
{
}



void CSoundWrap::SampleStop( int idx )
{
}


bool CSoundWrap::SamplePlaying( int idx )
{
	return false;
}

////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
/// Music functions 
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////

//
// ------------------------------------------------------------------------
// MusicLoad()
// Append new music to music list 
// For now if other music was loaded previously it has been stopped
// ------------------------------------------------------------------------
//
bool CSoundWrap::MusicLoad( /*const char* name*/ int name_idx ) 
{
	return true;
}	

//
// ------------------------------------------------------------------------
// MusicStart()
// ------------------------------------------------------------------------
//
void CSoundWrap::MusicStart( bool loop ) 
{
}

//
// ------------------------------------------------------------------------
// MusicStop()
// ------------------------------------------------------------------------
//
void CSoundWrap::MusicStop( ) 
{
}		

//
// ------------------------------------------------------------------------
// MusicPause()
// ------------------------------------------------------------------------
//
void CSoundWrap::MusicPause( const bool pause ) 
{
}

//
// ------------------------------------------------------------------------
// MusicPlaying()
// Return true, if music was playing.
// ------------------------------------------------------------------------
//
bool CSoundWrap::MusicPlaying( int id )
{
	return false;
}


bool CSoundWrap::MusicPlaying()
{
	return false;
}

//
// ------------------------------------------------------------------------
// MusicFree()
// ------------------------------------------------------------------------
//
void CSoundWrap::MusicFree() 
{
}	

#endif /* ENABLE_SOUND */
